package lib.util;

import java.util.ArrayList;

/**
 * Extended libraries for arrays of long
 * @author dolphinigle
 */
public abstract class ArraysLong {
  
  /**
   * Converts and Array of Long into Array of Int
   * @param list
   */
  public static int[] ArrayLongToArrayInt(long[] list) {
    int[] seq = new int[list.length];
    for (int i = 0; i < list.length; ++i) {
      seq[i] = (int)list[i];
    }
    return seq;
  }
  
  /**
   * Converts an ArrayList<Long> into long[]
   * @param list
   * @return
   */
  public static long[] ArrayListLongToArray(ArrayList<Long> list) {
    long[] ret = new long[list.size()];
    for (int i = 0; i < list.size(); ++i) ret[i] = list.get(i);
    return ret;
  }
  
  /**
   * Converts and ArrayList<Integer> into int[]
   */
  public static int[] ArrayListIntToArray(ArrayList<Integer> list) {
    int[] ret = new int[list.size()];
    for (int i = 0; i < list.size(); ++i) ret[i] = list.get(i);
    return ret;
  }

  /**
   * Reverses the content of param
   * @param param
   */
  public static void reverse(long[] param) {
    for (int i = 0; i < param.length / 2; ++i) {
      long tmp = param[i];
      param[i] = param[(param.length - i - 1)];
      param[(param.length - i - 1)] = tmp;
    }
  }

  /**
   * Reverses true iff arr contains param
   * @param arr array to be searched in
   * @param param target
   * @return true iff param is in arr
   */
  public static boolean contains(long[] arr, long param) {
    for (long num : arr) {
      if (num == param) return true;
    }
    return false;
  }
  
  /**
   * Analogous to c++'s insert. Does not modify inputs.
   * @param arr
   * @param position
   * @param value
   * @return
   */
  public static long[] Insert(long[] arr, int position, long value) {
    long[] ret = new long[arr.length + 1];
    int pos = 0;
    for (int i = 0; i < ret.length; ++i) {
      if (i == position) {
        ret[i] = value;
      } else {
        ret[i] = arr[pos];
        ++pos;
      }
    }
    return ret;
  }
  
  /**
   * Concats two long[]s
   */
  public static long[] Concat(long[] p1, long[] p2) {
    long[] ret = new long[p1.length + p2.length];
    for (int i = 0; i < p1.length; ++i) {
      ret[i] = p1[i];
    }
    for (int i = 0; i < p2.length; ++i) {
      ret[i + p1.length] = p2[i];
    }
    return ret;
  }

}
